<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2022/3/16
 */

namespace App\Http\Controllers;

use App\Filters\InfoFilter;
use App\Models\Info;
use App\Models\InfoCategory;
use App\Services\AdsService;
use App\Services\InfoCategoryService;
use App\Services\InfoService;
use App\Services\MetaService;

/**
 * 信息模块
 * Class InfoController
 * @package App\Http\Controllers
 */
class InfoController extends Controller
{

    /**
     * 栏目频道页
     * @param InfoCategory $cate
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    function index(InfoCategory $cate)
    {
        // 类型校验
        if ($cate->type != 1) abort(404);
        // 模板、数据
        $templateDIr = 'info-channel/';
        $template = $templateDIr . $cate->index_template;
        $template = $this->existView($template) ? $template : $templateDIr . 'index';
        $data = [];
        $data['meta'] = MetaService::cateMeta($cate);
        InfoCategoryService::setCateLink($cate);
        $data['category'] = $cate;
        $data['subCategory'] = InfoCategoryService::getAllSubsTreeByParentId($cate['id']);
        $data['breadcrumb'] = InfoCategoryService::getBreadcrumb($cate);
        $data['pageSize'] = self::PAGESIZE;
        $data['ads'] = AdsService::getAdsByInfoCategoryId($cate->id); // 栏目关联广告
        return $this->view($template, $data);
    }

    /**
     * 栏目频道页
     * @param InfoCategory $cate
     * @param InfoFilter $infoFilter
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    function list(InfoCategory $cate, InfoFilter $infoFilter)
    {
        // 类型校验
        if ($cate->type != 2) abort(404);

        // 模板、数据
        //- 默认列表模板
        $templateDIr = $cate->info_model_id == 2 ? 'info-picture/' : 'info-news/';
        $template = $templateDIr . $cate->list_template;
        $template = $this->existView($template) ? $template : $templateDIr . 'list';
        //- 判断后台是否设置了栏目首页模板
        if ($cate->index_template && !Request()->query('page')) {
            $template = $templateDIr . $cate->index_template;
            if (!$this->existView($template)) {
                $template = $templateDIr . 'index';
            }
        }
        $data = [];
        $data['meta'] = MetaService::cateMeta($cate);
        InfoCategoryService::setCateLink($cate);
        $data['category'] = $cate;
        $data['subCategory'] = InfoCategoryService::getAllSubsTreeByParentId($cate['id']);
        $data['breadcrumb'] = InfoCategoryService::getBreadcrumb($cate);
        $data['list'] = InfoService::getPageList([['info_category_id', $cate->id]], '', self::PAGESIZE, $infoFilter);
        $data['keywords'] = Request()->keywords ?? '';
        $data['ads'] = AdsService::getAdsByInfoCategoryId($cate->id); // 栏目关联广告
        return $this->view($template, $data);
    }

    /**
     * 信息详情页
     * @param InfoCategory $cate
     * @param Info $info
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    function show(InfoCategory $cate, Info $info)
    {
        // 信息的栏目合法性
        if ($info->info_category_id != $cate->id) abort(404);

        // 模板、数据
        $templateDIr = $cate->info_model_id == 2 ? 'info-picture/' : 'info-news/';
        $template = $templateDIr . $cate->article_template;
        $template = $this->existView($template) ? $template : $templateDIr . 'show';
        $data = [];
        $data['meta'] = MetaService::infoMeta($info);
        InfoCategoryService::setCateLink($cate);
        $data['category'] = $cate;
        $data['subCategory'] = InfoCategoryService::getAllSubsTreeByParentId($cate['id']);
        $data['breadcrumb'] = InfoService::getBreadcrumb($info);
        //- 图集模块图片列表
        if ($info->info_model_id == 2) {
            $info['image_list'] = InfoService::getImageList($info);
        }
        InfoService::addHits($info); // 新增点击量
        $info['hits'] = InfoService::liveHits($info); // 实时点击量
        $data['detail'] = $info;
        //- 最新文章：10条
        $data['newList'] = InfoService::getList([['info_category_id', $cate['id']]], 10);
        //- 上下篇
        $data['prev'] = InfoService::getPrev($info);
        $data['next'] = InfoService::getNext($info);
        // 栏目关联广告
        $data['ads'] = AdsService::getAdsByInfoCategoryId($cate->id);
        return $this->view($template, $data);
    }
}
